home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH10 / SRC / HILO.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-05-02  |  16.7 KB  |  564 lines

  1. VERSION 4.00
  2. Begin VB.Form HiLoForm 
  3.    Appearance      =   0  'Flat
  4.    BackColor       =   &H00C0C0C0&
  5.    Caption         =   "Hi-Lo"
  6.    ClientHeight    =   5700
  7.    ClientLeft      =   300
  8.    ClientTop       =   855
  9.    ClientWidth     =   9090
  10.    BeginProperty Font 
  11.       name            =   "MS Sans Serif"
  12.       charset         =   1
  13.       weight          =   700
  14.       size            =   8.25
  15.       underline       =   0   'False
  16.       italic          =   0   'False
  17.       strikethrough   =   0   'False
  18.    EndProperty
  19.    ForeColor       =   &H80000008&
  20.    Height          =   6390
  21.    KeyPreview      =   -1  'True
  22.    Left            =   240
  23.    LinkTopic       =   "Form1"
  24.    ScaleHeight     =   5700
  25.    ScaleWidth      =   9090
  26.    Top             =   225
  27.    Width           =   9210
  28.    Begin VB.OptionButton Choice 
  29.       Caption         =   "Sombrero"
  30.       Height          =   255
  31.       Index           =   9
  32.       Left            =   7080
  33.       TabIndex        =   18
  34.       Top             =   3240
  35.       Width           =   2055
  36.    End
  37.    Begin VB.CheckBox ShowHiddenCheck 
  38.       Caption         =   "Show Hidden Surfaces"
  39.       Height          =   495
  40.       Left            =   7080
  41.       TabIndex        =   17
  42.       Top             =   4440
  43.       Width           =   2055
  44.    End
  45.    Begin VB.CheckBox ShowAxesCheck 
  46.       Caption         =   "Show Axes"
  47.       Height          =   255
  48.       Left            =   7080
  49.       TabIndex        =   16
  50.       Top             =   4080
  51.       Width           =   2055
  52.    End
  53.    Begin VB.OptionButton Choice 
  54.       Caption         =   "Saddle"
  55.       Height          =   255
  56.       Index           =   8
  57.       Left            =   7080
  58.       TabIndex        =   15
  59.       Top             =   2880
  60.       Width           =   2055
  61.    End
  62.    Begin VB.OptionButton Choice 
  63.       Caption         =   "Cone"
  64.       Height          =   255
  65.       Index           =   7
  66.       Left            =   7080
  67.       TabIndex        =   14
  68.       Top             =   2520
  69.       Width           =   2055
  70.    End
  71.    Begin VB.OptionButton Choice 
  72.       Caption         =   "Holes"
  73.       Height          =   255
  74.       Index           =   6
  75.       Left            =   7080
  76.       TabIndex        =   13
  77.       Top             =   2160
  78.       Width           =   2055
  79.    End
  80.    Begin VB.TextBox PhiText 
  81.       Height          =   285
  82.       Left            =   3600
  83.       TabIndex        =   12
  84.       Text            =   "0.1570"
  85.       Top             =   5400
  86.       Width           =   855
  87.    End
  88.    Begin VB.TextBox ThetaText 
  89.       Height          =   285
  90.       Left            =   2040
  91.       TabIndex        =   10
  92.       Text            =   "0.6283"
  93.       Top             =   5400
  94.       Width           =   855
  95.    End
  96.    Begin VB.TextBox RText 
  97.       Height          =   285
  98.       Left            =   480
  99.       TabIndex        =   8
  100.       Text            =   "10"
  101.       Top             =   5400
  102.       Width           =   855
  103.    End
  104.    Begin VB.OptionButton Choice 
  105.       Caption         =   "Hemisphere"
  106.       Height          =   255
  107.       Index           =   5
  108.       Left            =   7080
  109.       TabIndex        =   7
  110.       Top             =   1800
  111.       Width           =   2055
  112.    End
  113.    Begin VB.OptionButton Choice 
  114.       Caption         =   "Randomized Ridges"
  115.       Height          =   255
  116.       Index           =   4
  117.       Left            =   7080
  118.       TabIndex        =   6
  119.       Top             =   1440
  120.       Width           =   2055
  121.    End
  122.    Begin VB.OptionButton Choice 
  123.       Caption         =   "Ridges"
  124.       Height          =   255
  125.       Index           =   3
  126.       Left            =   7080
  127.       TabIndex        =   5
  128.       Top             =   1080
  129.       Width           =   2055
  130.    End
  131.    Begin VB.OptionButton Choice 
  132.       Caption         =   "Bowl"
  133.       Height          =   255
  134.       Index           =   2
  135.       Left            =   7080
  136.       TabIndex        =   4
  137.       Top             =   720
  138.       Width           =   2055
  139.    End
  140.    Begin VB.OptionButton Choice 
  141.       Caption         =   "Mounds"
  142.       Height          =   255
  143.       Index           =   1
  144.       Left            =   7080
  145.       TabIndex        =   3
  146.       Top             =   360
  147.       Width           =   2055
  148.    End
  149.    Begin VB.OptionButton Choice 
  150.       Caption         =   "Splash"
  151.       Height          =   255
  152.       Index           =   0
  153.       Left            =   7080
  154.       TabIndex        =   2
  155.       Top             =   0
  156.       Value           =   -1  'True
  157.       Width           =   2055
  158.    End
  159.    Begin VB.PictureBox Pict 
  160.       AutoRedraw      =   -1  'True
  161.       Height          =   5295
  162.       Left            =   0
  163.       ScaleHeight     =   349
  164.       ScaleMode       =   3  'Pixel
  165.       ScaleWidth      =   461
  166.       TabIndex        =   0
  167.       Top             =   0
  168.       Width           =   6975
  169.    End
  170.    Begin MSComDlg.CommonDialog LoadDialog 
  171.       Left            =   7080
  172.       Top             =   5040
  173.       _version        =   65536
  174.       _extentx        =   847
  175.       _extenty        =   847
  176.       _stockprops     =   0
  177.       cancelerror     =   -1  'True
  178.    End
  179.    Begin VB.Label Label1 
  180.       Caption         =   "Phi"
  181.       Height          =   255
  182.       Index           =   2
  183.       Left            =   3240
  184.       TabIndex        =   11
  185.       Top             =   5400
  186.       Width           =   375
  187.    End
  188.    Begin VB.Label Label1 
  189.       Caption         =   "Theta"
  190.       Height          =   255
  191.       Index           =   1
  192.       Left            =   1440
  193.       TabIndex        =   9
  194.       Top             =   5400
  195.       Width           =   495
  196.    End
  197.    Begin VB.Label Label1 
  198.       Caption         =   "R"
  199.       Height          =   255
  200.       Index           =   0
  201.       Left            =   240
  202.       TabIndex        =   1
  203.       Top             =   5400
  204.       Width           =   255
  205.    End
  206.    Begin VB.Menu mnuFile 
  207.       Caption         =   "&File"
  208.       Begin VB.Menu mnuFileLoad 
  209.          Caption         =   "&Load..."
  210.          Shortcut        =   ^L
  211.       End
  212.       Begin VB.Menu mnuFileSaveAs 
  213.          Caption         =   "&Save As..."
  214.          Shortcut        =   ^A
  215.       End
  216.       Begin VB.Menu mnuFileSep 
  217.          Caption         =   "-"
  218.       End
  219.       Begin VB.Menu mnuFileExit 
  220.          Caption         =   "E&xit"
  221.       End
  222.    End
  223. Attribute VB_Name = "HiLoForm"
  224. Attribute VB_Creatable = False
  225. Attribute VB_Exposed = False
  226. Option Explicit
  227. ' Location of viewing eye.
  228. Dim EyeR As Single
  229. Dim EyeTheta As Single
  230. Dim EyePhi As Single
  231. Const Dtheta = PI / 20
  232. Const Dphi = PI / 20
  233. Const Dr = 1
  234. ' Location of focus point.
  235. Const FocusX = 0#
  236. Const FocusY = 0#
  237. Const FocusZ = 0#
  238. Dim Projector(1 To 4, 1 To 4) As Single
  239. Dim ThePicture As ObjPicture
  240. Dim TheGrid As ObjGrid3D
  241. Dim ShowingParameters As Boolean
  242. Dim ChoiceNum As Integer
  243. ' *******************************************************
  244. ' Draw the surface.
  245. ' *******************************************************
  246. Private Sub DrawData(pic As Object)
  247. Dim x As Single
  248. Dim y As Single
  249. Dim z As Single
  250. Dim S(1 To 4, 1 To 4) As Single
  251. Dim t(1 To 4, 1 To 4) As Single
  252. Dim ST(1 To 4, 1 To 4) As Single
  253. Dim PST(1 To 4, 1 To 4) As Single
  254.     MousePointer = vbHourglass
  255.     Refresh
  256.     ' Prevent overflow errors when drawing lines
  257.     ' too far out of bounds.
  258.     On Error Resume Next
  259.     ' Scale and translate so it looks OK in pixels.
  260.     m3Scale S, 35, -35, 1
  261.     m3Translate t, 230, 175, 0
  262.     m3MatMultiplyFull ST, S, t
  263.     m3MatMultiplyFull PST, Projector, ST
  264.     ' Transform the points.
  265.     ThePicture.ApplyFull PST
  266.     ' Display the data.
  267.     pic.Cls
  268.     ThePicture.Draw pic, EyeR
  269.     pic.Refresh
  270.     ' Display the viewing parameters.
  271.     ShowViewingParameters
  272.     MousePointer = vbDefault
  273. End Sub
  274. Sub ShowViewingParameters()
  275.     ShowingParameters = True
  276.     RText.Text = Format$(EyeR, "0.0000")
  277.     ThetaText.Text = Format$(EyeTheta, "0.0000")
  278.     PhiText.Text = Format$(EyePhi, "0.0000")
  279.     RText.Refresh
  280.     ThetaText.Refresh
  281.     PhiText.Refresh
  282.     ShowingParameters = False
  283. End Sub
  284. Private Sub Choice_Click(Index As Integer)
  285.     ChoiceNum = Index
  286.     CreateData (ShowAxesCheck.value = vbChecked)
  287.     DrawData Pict
  288.     Pict.SetFocus
  289. End Sub
  290. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  291.     Select Case KeyCode
  292.         Case vbKeyLeft
  293.             EyeTheta = EyeTheta - Dtheta
  294.         
  295.         Case vbKeyRight
  296.             EyeTheta = EyeTheta + Dtheta
  297.         
  298.         Case vbKeyUp
  299.             EyePhi = EyePhi - Dphi
  300.         
  301.         Case vbKeyDown
  302.             EyePhi = EyePhi + Dphi
  303.                 
  304.         Case Else
  305.             Exit Sub
  306.     End Select
  307.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  308.     DrawData Pict
  309. End Sub
  310. Private Sub Form_KeyPress(KeyAscii As Integer)
  311.     Select Case KeyAscii
  312.         Case Asc("+")
  313.             EyeR = EyeR + Dr
  314.         
  315.         Case Asc("-")
  316.             EyeR = EyeR - Dr
  317.         
  318.         Case Else
  319.             Exit Sub
  320.     End Select
  321.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  322.     DrawData Pict
  323. End Sub
  324. Private Sub Form_Load()
  325.     ' Initialize the eye position.
  326.     EyeR = 10
  327.     EyeTheta = PI * 0.2
  328.     EyePhi = PI * 0.1
  329.     ' Initialize the projection transformation.
  330.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  331.     ' Create the data.
  332.     CreateData (ShowAxesCheck.value = vbChecked)
  333.     ' Project and draw the data.
  334.     Me.Show
  335.     DrawData Pict
  336. End Sub
  337. ' ************************************************
  338. ' Create the surface.
  339. ' ************************************************
  340. Sub CreateData(show_axes As Boolean)
  341. Const xmin = -5
  342. Const Zmin = -5
  343. Const dx = 0.3
  344. Const dz = 0.3
  345. Const NumX = -2 * xmin / dx
  346. Const NumZ = -2 * Zmin / dz
  347. Const Amp = 0.25
  348. Const Per = 2 * PI / 4
  349. Const Amp2 = 1
  350. Const Per2 = 2 * PI / 16
  351. Const Amp3 = 2
  352. Dim axis As ObjPolyline
  353. Dim i As Integer
  354. Dim j As Integer
  355. Dim x As Single
  356. Dim y As Single
  357. Dim z As Single
  358. Dim D As Single
  359. Dim R2 As Single
  360. Dim x1 As Single
  361. Dim z1 As Single
  362. Dim x2 As Single
  363. Dim z2 As Single
  364.     MousePointer = vbHourglass
  365.     Refresh
  366.     Set ThePicture = New ObjPicture
  367.     Set TheGrid = New ObjGrid3D
  368.     TheGrid.SetBounds xmin, dx, NumX, Zmin, dz, NumZ
  369.     ThePicture.objects.Add TheGrid
  370.     TheGrid.ShowHidden = (ShowHiddenCheck.value = vbChecked)
  371.     If show_axes Then
  372.         Set axis = New ObjPolyline
  373.         ThePicture.objects.Add axis
  374.         axis.AddSegment 0, 0, 0, 5.5, 0, 0
  375.         axis.AddSegment 0, 0, 0, 0, 3, 0
  376.         axis.AddSegment 0, 0, 0, 0, 0, 5.5
  377.     End If
  378.     R2 = (xmin + 3 * dx) * (xmin + 3 * dx)
  379.     x = xmin
  380.     For i = 1 To NumX
  381.         z = Zmin
  382.         For j = 1 To NumZ
  383.             Select Case ChoiceNum
  384.                 Case 0  ' Splash.
  385.                     D = Sqr(x * x + z * z)
  386.                     y = Amp * Cos(3 * D)
  387.                 Case 1  ' Mounds.
  388.                     y = Amp * (Cos(Per * x) + Cos(Per * z))
  389.                 
  390.                 Case 2  ' Bowl.
  391.                     y = 0.2 * (x * x + z * z) - 5#
  392.                 
  393.                 Case 3  ' Ridges.
  394.                     y = Amp2 * Cos(Per2 * x) + Amp3 * Cos(Per * z) / (Abs(z) / 3 + 1)
  395.             
  396.                 Case 4  ' Random ridges.
  397.                     y = Amp2 * Cos(Per2 * x) + Amp3 * Cos(Per * z) / (Abs(z) / 3 + 1) + Amp * Rnd
  398.             
  399.                 Case 5  ' Hemisphere.
  400.                     D = x * x + z * z
  401.                     If D >= R2 Then
  402.                         y = 0
  403.                     Else
  404.                         y = Sqr(R2 - D)
  405.                     End If
  406.                 
  407.                 Case 6  ' Holes.
  408.                     x1 = (x + xmin / 2)
  409.                     z1 = (z + xmin / 2)
  410.                     x2 = (x - xmin / 2)
  411.                     z2 = (z - xmin / 2)
  412.                     y = Amp3 - _
  413.                 1 / (x1 * x1 + z1 * z1 + 0.1) - _
  414.                 1 / (x2 * x2 + z1 * z1 + 0.1) - _
  415.                 1 / (x1 * x1 + z2 * z2 + 0.1) - _
  416.                 1 / (x2 * x2 + z2 * z2 + 0.1)
  417.             
  418.                 Case 7  ' Cone.
  419.                     D = 2 * (Amp3 - Sqr(x * x + z * z))
  420.                     If D < -Amp3 Then D = -Amp3
  421.                     y = D
  422.             
  423.                 Case 8  ' Saddle.
  424.                     y = (x * x - z * z) / 10
  425.                 
  426.                 Case 9  ' Sombrero.
  427.                     D = Sqr(x * x + z * z)
  428.                     If D < 1 Then
  429.                         R2 = 10
  430.                     Else
  431.                         R2 = 10 / D
  432.                     End If
  433.                     y = R2 * Amp * Cos(1.5 * D)
  434.             End Select
  435.             
  436.             TheGrid.SetValue x, y, z
  437.             z = z + dz
  438.         Next j
  439.         x = x + dx
  440.     Next i
  441.     MousePointer = vbDefault
  442. End Sub
  443. Private Sub mnuFileExit_Click()
  444.     Unload Me
  445. End Sub
  446. Private Sub mnuFileLoad_Click()
  447. Dim fname As String
  448. Dim filenum As Integer
  449. Dim txt As String
  450. Dim xmin As Single
  451. Dim ymin As Single
  452. Dim xmax As Single
  453. Dim ymax As Single
  454.     ' Allow the user to pick a file.
  455.     On Error Resume Next
  456.     LoadDialog.filename = "*.APF"
  457.     LoadDialog.ShowOpen
  458.     If Err.Number = cdlCancel Then
  459.         Unload LoadDialog
  460.         Exit Sub
  461.     ElseIf Err.Number <> 0 Then
  462.         Unload LoadDialog
  463.         Beep
  464.         MsgBox "Error selecting file.", , vbExclamation
  465.         Exit Sub
  466.     End If
  467.     On Error GoTo 0
  468.     fname = LoadDialog.filename
  469.     LoadDialog.InitDir = Left$(fname, Len(fname) _
  470.         - Len(LoadDialog.FileTitle) - 1)
  471.     ' Clear the picture.
  472.     Set ThePicture = Nothing
  473.     ' Open the file.
  474.     filenum = FreeFile
  475.     Open fname For Input As #filenum
  476.     ' Make sure it's an Object Picture File.
  477.     Input #filenum, txt
  478.     If txt <> "3D APF PICTURE" Then
  479.         Close filenum
  480.         Beep
  481.         MsgBox "Error reading file """ & fname & """.", , vbExclamation
  482.         Exit Sub
  483.     End If
  484.     ' Read the picture.
  485.     MousePointer = vbHourglass
  486.     DoEvents
  487.     Set ThePicture = New ObjPicture
  488.     ThePicture.FileInput filenum
  489.     ' Close the file.
  490.     Close filenum
  491.     If ThePicture.objects(1).ObjectType = "GRID" Then
  492.         Set TheGrid = ThePicture.objects(1)
  493.         TheGrid.ShowHidden = (ShowHiddenCheck.value = vbChecked)
  494.     End If
  495.     ' Refresh the display.
  496.     DrawData Pict
  497.     ' Deselect all the option buttons.
  498.     For ChoiceNum = 0 To 9
  499.         If Choice(ChoiceNum).value Then _
  500.             Choice(ChoiceNum).value = False
  501.     Next ChoiceNum
  502.     MousePointer = vbDefault
  503. End Sub
  504. Private Sub mnuFileSaveAs_Click()
  505. Dim fname As String
  506. Dim filenum As Integer
  507.     ' Allow the user to pick a file.
  508.     On Error Resume Next
  509.     LoadDialog.filename = "*.APF"
  510.     LoadDialog.Flags = cdlOFNOverwritePrompt + cdlOFNHideReadOnly
  511.     LoadDialog.ShowSave
  512.     If Err.Number = cdlCancel Then
  513.         Unload LoadDialog
  514.         Exit Sub
  515.     ElseIf Err.Number <> 0 Then
  516.         Unload LoadDialog
  517.         Beep
  518.         MsgBox "Error selecting file.", , vbExclamation
  519.         Exit Sub
  520.     End If
  521.     On Error GoTo 0
  522.     fname = LoadDialog.filename
  523.     LoadDialog.InitDir = Left$(fname, Len(fname) _
  524.         - Len(LoadDialog.FileTitle) - 1)
  525.     ' Open the file.
  526.     filenum = FreeFile
  527.     Open fname For Output As #filenum
  528.     ' Write the picture.
  529.     ThePicture.FileWrite filenum
  530.     ' Close the file.
  531.     Close filenum
  532. End Sub
  533. Private Sub PhiText_Change()
  534.     If ShowingParameters Then Exit Sub
  535.     EyePhi = CSng(PhiText.Text)
  536.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  537.     DrawData Pict
  538. End Sub
  539. ' ************************************************
  540. ' Turn hidden surfaces on or off.
  541. ' ************************************************
  542. Private Sub ShowHiddenCheck_Click()
  543.     TheGrid.ShowHidden = (ShowHiddenCheck.value = vbChecked)
  544.     DrawData Pict
  545.     Pict.SetFocus
  546. End Sub
  547. Private Sub RText_Change()
  548.     If ShowingParameters Then Exit Sub
  549.     EyeR = CSng(RText.Text)
  550.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  551.     DrawData Pict
  552. End Sub
  553. Private Sub ShowAxesCheck_Click()
  554.     CreateData (ShowAxesCheck.value = vbChecked)
  555.     DrawData Pict
  556.     Pict.SetFocus
  557. End Sub
  558. Private Sub ThetaText_Change()
  559.     If ShowingParameters Then Exit Sub
  560.     EyeTheta = CSng(ThetaText.Text)
  561.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  562.     DrawData Pict
  563. End Sub
  564.